package com.vviton.zyhj.admin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vviton.zyhj.admin.service.ZjzfxxPtService;
import com.vviton.zyhj.common.common.CodeConstants;
import com.vviton.zyhj.common.content.SpringContext;
import com.vviton.zyhj.common.view.ParamZjzfxx;

/**
 * ClassName: BatchTransSuccess <br/>
 * Function: 批量付款响应处理. <br/>
 * Date: 2015年11月18日 <br/>
 *
 * @author zhangjin
 * @version 1.0
 * @since JDK 1.7
 */
@WebServlet("/BatchTransSuccess")
public class BatchTransSuccess extends HttpServlet {

    /** 序列号 */
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BatchTransSuccess() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * doGet,批量付款响应get请求. <br/>
     * Author: zhangjin <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * doPost,批量付款响应post请求. <br/>
     * Author: zhangjin <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws ServletException 异常
     * @throws IOException 异常
     * @since JDK 1.7
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        ZjzfxxPtService zjzfxxPtService = SpringContext.getBean(ZjzfxxPtService.class);
        //        PlfkdddygxService plfkdddygxService = SpringContext.getBean(PlfkdddygxService.class);
        // 交易批次号
        String batchNo = request.getParameter("batch_no");
        // 转账成功的详细信息
        String successDetails = request.getParameter("success_details");
        // 转账失败的详细信息
        String failDetails = request.getParameter("fail_details");
        //从会话取得会员信息
        //        SessionYWRY ywry = CommonConstants.getSessionYWRY(request);
        // 平台登录账号
        String zh = "1000000012";
        /*if (ywry != null) {
            if (ywry.getZh() != null && ywry.getZh().length() > 0) {
                zh = ywry.getZh();
            }
        }*/
        ZjzfxxPtService zjzfxxPtService = SpringContext.getBean(ZjzfxxPtService.class);
        //Map<String, List<TYyxtJyglPlfkdddygx>> successInfo = new HashMap<String, List<TYyxtJyglPlfkdddygx>>();
        //Map<String, List<TYyxtJyglPlfkdddygx>> failInfo = new HashMap<String, List<TYyxtJyglPlfkdddygx>>();
        //List<ParamPlfkxx> plfkxxs = new ArrayList<ParamPlfkxx>();
        if (successDetails != null) {
            String[] skttxx = successDetails.split("\\|");
            if (skttxx != null && skttxx.length > 0) {
                for (String skkt : skttxx) {
                    /*
                     * 截取支付宝返回信息
                     * en.getKey() + "^13771904149^张金^" + ysje + "^付款到商家|"
                     * 0-流水号（课堂编号KTID）
                     * 1-收款方账号
                     * 2-收款账号姓名
                     * 3-付款金额
                     * 4-成功标识(S)
                     * 5-成功原因(null)
                     * 6-支付宝内部流水号
                     * 7-完成时间（yyyyMMddHHmmss）
                     */
                    String[] skxx = skkt.split("\\^");
                    if (skxx != null && skxx.length > 0) {
                        //List<TYyxtJyglPlfkdddygx> plfkdddy = plfkdddygxService.getPlfkdddygxByKtidAndJspch(skxx[0], batchNo);
                        //successInfo.put(skxx[0], plfkdddy);

                        ParamZjzfxx paramZjzfxx = new ParamZjzfxx();
                        System.out.println("金额：" + skxx[3]);
                        String zfje = skxx[3];
                        paramZjzfxx.setKtid(skxx[0]);
                        paramZjzfxx.setZh(zh);
                        paramZjzfxx.setZfje(skxx[3]);
                        paramZjzfxx.setZjjssj(skxx[7]);
                        paramZjzfxx.setSkfzh(skxx[1]);
                        paramZjzfxx.setSkfxm(skxx[2]);
                        paramZjzfxx.setZfjyh(skxx[6]);
                        //plfkxxs.add(paramZjzfxx);
                        // 资金支付信息（平台）
                        zjzfxxPtService.insertZjzfxxPt(paramZjzfxx);
                        //结算记录
                        paramZjzfxx.setFlag(CodeConstants.JSZT_YJS);
                        //zjzfxxPtService.updateJsjlb(paramZjzfxx);
                    }

                }
            }
        }

        if (failDetails != null) {
            String[] skttxx = failDetails.split("\\|");
            if (skttxx != null && skttxx.length > 0) {
                for (String skkt : skttxx) {
                    /*
                     * 截取支付宝返回信息
                     * 0-流水号（课堂编号KTID）
                     * 1-收款方账号
                     * 2-收款账号姓名
                     * 3-付款金额
                     * 4-成功标识(S)
                     * 5-成功原因(null)
                     * 6-支付宝内部流水号
                     * 7-完成时间（yyyyMMddHHmmss）
                     */
                    String[] skxx = skkt.split("\\^");
                    if (skxx != null && skxx.length > 0) {
                        //List<TYyxtJyglPlfkdddygx> plfkdddy = plfkdddygxService.getPlfkdddygxByKtidAndJspch(skxx[0], batchNo);
                        //failInfo.put(skxx[0], plfkdddy);
                        ParamZjzfxx paramJssb = new ParamZjzfxx();
                        paramJssb.setKtid(skxx[0]);
                        paramJssb.setZh(zh);
                        paramJssb.setZfje(skxx[3]);
                        paramJssb.setZjjssj(skxx[7]);
                        paramJssb.setZfjyh(skxx[6]);
                        //失败
                        paramJssb.setFlag(CodeConstants.JSZT_SB);
                        zjzfxxPtService.updateJsjlb(paramJssb);
                    }

                }
            }
        }
        System.out.println("------成功-----");
    }

}
